package com.example.login;

import java.sql.Timestamp;
import java.util.Date;

import com.example.component.Window.HomeWindow;
import com.example.component.Window.LoginDialog;
import com.example.hibernate.core.HibernateCommunicator;
import com.example.hibernate.core.TalkToDB;
import com.example.hibernate.pojos.IUserOperation;
import com.example.hibernate.pojos.IUsers;
import com.example.hibernate.pojos.UserOperation;
import com.example.service.AbstractService;
import com.example.service.IService;
import com.vaadin.Application;

public class LoginService extends AbstractService implements ILoginService {

	private static LoginService _instance = null;

	private LoginService() {

	}

	public static LoginService getInstance() {
		if (_instance == null)
			_instance = new LoginService();
		return _instance;
	}

	public int validateLogin(String userName, String password, Application app) {
		ILoggedInUserDetails existingLoggedInUser = null;
		// ILoggedInUserDetails existingLoggedInUser = (ILoggedInUserDetails)
		// app
		// .getUser();
		if (app.getUser() != null)
			app.setUser(null);

		// if (existingLoggedInUser == null) {
		// This is a new Login
		IUsers p = (IUsers) TalkToDB.getInstance().getUserPasswordForUserName(
				userName);
		if (p != null) {
			// if(p.validate(userName, password)){
			if (p.validatePassword(password)) {
				switch (TalkToDB.getInstance().isTheUserAlreadyLoggedIn(userName)) {
				case IUserOperation.LOGGEDIN:
					return ILoginService.ALREADY_LOGGEDIN;
				case IUserOperation.BANNED:
					return ILoginService.USER_BANNED;
				case IUserOperation.LOCKED:
					return ILoginService.USER_LOCKED;
				case IUserOperation.LOGGEDOUT: {
					existingLoggedInUser = new LoggedInUserDetails();
					existingLoggedInUser.setUsers(p);
					existingLoggedInUser.setApp(app);
					app.setUser(existingLoggedInUser);
					UserOperation uo = new UserOperation();
					// uo.setFfid(100);
					uo.setUserName(userName);
					uo.setLoginStatus(IUserOperation.LOGGEDIN);
					uo.setTimeStamp(new Timestamp(new Date().getTime()));
					TalkToDB.getInstance().addUserLogin(uo);
					return ILoginService.LOGIN_SUCCESS;
				}
				default:
					return ILoginService.LOGIN_FAILED_UNKNOWN_REASON;
				}
			} else {
				app.setUser(null);
				return ILoginService.LOGIN_FAILED_BAD_PASSWORD;
			}
		} else {
			return ILoginService.LOGIN_FAILED_UNKNOWN_USER;
		}
		// }
	}

	public void init() {
		// TODO Auto-generated method stub

	}

	@Override
	public String serviceName() {
		return IService.LOGINSERVICE;
	}

	public void showLoginDialog(Application app) {
		app.getMainWindow().addWindow(new LoginDialog());

	}

	public void logout(Application app, boolean auto) {
		ILoggedInUserDetails user = (ILoggedInUserDetails) app.getUser();
		if(user == null){
			if(!auto)
				app.close();
			return;
		}

		IUsers u = (IUsers) user.getUsers();
		if (u != null) {
			IUserOperation uo = new UserOperation();
			uo.setUserName(u.getUserName());
			uo.setLoginStatus(IUserOperation.LOGGEDOUT);
			uo.setTimeStamp(new Timestamp(new Date().getTime()));
			TalkToDB.getInstance().addUserLogin(uo);
			app.setUser(null);
			if(!auto)
				app.close();
			// ((HomeWindow)app.getMainWindow()).initWindow();
		}
	}

	public void logoutAllUsers() {
		// TODO Auto-generated method stub

	}
}
